package com.ruoyi.web.controller.bkcontroller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DailyWork;
import com.ruoyi.system.service.IDailyWorkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 日报Controller
 *
 * @author ruoyi
 * @date 2021-08-13
 */
@RestController
@RequestMapping("/system/work")
public class DailyWorkController extends BaseController {
    @Autowired
    private IDailyWorkService dailyWorkService;

    /**
     * 查询日报列表
     */
    @PreAuthorize("@ss.hasPermi('system:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(DailyWork dailyWork) {
        startPage();
        List<DailyWork> list = dailyWorkService.selectDailyWorkList(dailyWork);

        String nickName = dailyWork.getNickName();

        String nName = getLoginUser().getUser().getNickName();

        //如果搜索日报是别人的nickName就不要添加自己的日志对象了
        if (nickName == null || nName.contains(nickName)) {

            //循环判断是否有今天的日报，没有则添加一个
            boolean hadMyDailyWork = false;

            for (DailyWork work : list) {
                if (getUsername().equals(work.getCreateBy()) && DateUtils.isSameDay(new Date(), work.getCreateTime())) {
                    hadMyDailyWork = true;
                    break;
                }
            }

            //没有今天的日报则补充一个日报进去
            if (hadMyDailyWork == false) {
                DailyWork myDailyWork = new DailyWork();
                myDailyWork.setNickName(nName);
                myDailyWork.setCreateTime(new Date());
                list.add(0, myDailyWork);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出日报列表
     */
    @PreAuthorize("@ss.hasPermi('system:work:export')")
    @Log(title = "日报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DailyWork dailyWork) {
        List<DailyWork> list = dailyWorkService.selectDailyWorkList(dailyWork);
        ExcelUtil<DailyWork> util = new ExcelUtil<DailyWork>(DailyWork.class);
        return util.exportExcel(list, "日报数据");
    }

    /**
     * 获取日报详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:work:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dailyWorkService.selectDailyWorkById(id));
    }

    /**
     * 新增日报
     */
    @PreAuthorize("@ss.hasPermi('system:work:add')")
    @Log(title = "日报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DailyWork dailyWork) {
        dailyWork.setUid(getUserId());
        dailyWork.setCreateBy(getUsername());
        return toAjax(dailyWorkService.insertDailyWork(dailyWork));
    }

    /**
     * 修改日报
     */
    @PreAuthorize("@ss.hasPermi('system:work:edit')")
    @Log(title = "日报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DailyWork dailyWork) {
        if (!getUserId().equals(dailyWork.getUid())) {
            return AjaxResult.error("不允许修改他人日报！");
        }
        dailyWork.setUpdateBy(getUsername());
        return toAjax(dailyWorkService.updateDailyWork(dailyWork));
    }

    /**
     * 删除日报
     */
    @PreAuthorize("@ss.hasPermi('system:work:remove')")
    @Log(title = "日报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dailyWorkService.deleteDailyWorkByIds(ids));
    }
}
